package Dao.Student.Imp;

import Dao.Student.StudentPaperDao;
import Entity.Grade.FinalGrade;
import Entity.Process.Report;
import Entity.Process.ReportDetail;
import Entity.TitleAndPaper.Paper;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentPaperDaoImp implements StudentPaperDao {
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    @Override
    public int insertPaper(Paper paper, Connection conn) {
        int result = 0;
        try {
            String sql = "insert into paper(cid, sno,tno,title,state,xt_id,bz)\n" +
                    "values (?,?,?,?,'任务书',?,'')";
            ps = conn.prepareStatement(sql);
            ps.setString(1, paper.getCid());
            ps.setString(2, paper.getSno());
            ps.setString(3, paper.getTno());
            ps.setString(4, paper.getTitle());
            ps.setObject(5, paper.getXtId());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }

    /**
     * 修改开题报告详情
     *
     * @param reportDetail
     * @param conn
     */
    @Override
    public int updateReportDetail(ReportDetail reportDetail, Connection conn) {
        int result = 0;
        try {
            String sql = "update report_detail set  sence=?,develop=?,content=?,scheme=?,aim=?,datum=? \n" +
                    "where report_id=?";
            ps = conn.prepareStatement(sql);

            ps.setString(1, reportDetail.getSence());
            ps.setString(2, reportDetail.getDevelop());
            ps.setString(3, reportDetail.getContent());
            ps.setObject(4, reportDetail.getScheme());
            ps.setObject(5, reportDetail.getAim());
            ps.setObject(6, reportDetail.getDatum());
            ps.setObject(7, reportDetail.getReportId());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;

    }

    /**
     * 修改开题报告
     *
     * @param report
     * @param conn
     */
    @Override
    public int updateReport(Report report, Connection conn) {
        int result = 0;
        try {
            String sql = "update report set state='审核中',is_pass='' where report_id=?";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, report.getReportId());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }

    /**
     * 向开题报告表插入一条数据
     *
     * @param report
     * @param conn
     */
    @Override
    public int insertReport(Report report, Connection conn) {
        int result = 0;
        try {
            String sql = "insert into report(title, collname,mname,sno,tno,state)\n" +
                    "values (?,?,?,?,?,'审核中')";
            ps = conn.prepareStatement(sql);
            ps.setString(1, report.getTitle());
            ps.setString(2, report.getCollName());
            ps.setString(3, report.getMName());
            ps.setString(4, report.getSno());
            ps.setString(5, report.getTno());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }

    /**
     * 向开题报告详情表插入一条数据
     *
     * @param reportDetail
     * @param conn
     */
    @Override
    public int insetReportDetail(ReportDetail reportDetail, Connection conn) {
        int result = 0;
        try {
            String sql = "insert into report_Detail(report_id, sence,develop,content,scheme,aim,datum)\n" +
                    "values (?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, reportDetail.getReportId());
            ps.setString(2, reportDetail.getSence());
            ps.setString(3, reportDetail.getDevelop());
            ps.setString(4, reportDetail.getContent());
            ps.setObject(5, reportDetail.getScheme());
            ps.setObject(6, reportDetail.getAim());
            ps.setObject(7, reportDetail.getDatum());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }

    /**
     * 根据reportId查询开题报告详情
     */
    @Override
    public ReportDetail getReportDetail(Integer reportId, Connection conn) {
        ReportDetail reportDetail = null;
        String sql = "select report_id,sence,develop,content,scheme,aim,datum,s_file_name,s_file_url,t_file_name,t_file_url " +
                "from report_detail where report_id=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1, reportId);
            rs = ps.executeQuery();
            if (rs.next()) {
                reportDetail = new ReportDetail();
                reportDetail.setReportId(rs.getInt("report_id"));
                reportDetail.setSence(rs.getString("sence"));
                reportDetail.setDevelop(rs.getString("develop"));
                reportDetail.setContent(rs.getString("content"));
                reportDetail.setScheme(rs.getString("scheme"));
                reportDetail.setAim(rs.getString("aim"));
                reportDetail.setDatum(rs.getString("datum"));
                reportDetail.setSFileName(rs.getString("s_file_name"));
                reportDetail.setSFileUrl(rs.getString("s_file_url"));
                reportDetail.setTFileName(rs.getString("t_file_name"));
                reportDetail.setTFileUrl(rs.getString("t_file_url"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.closeMySQLResource(ps,rs);
        }
        return reportDetail;
    }

    /**
     * 根据学号查询总评表
     */
    @Override
    public FinalGrade getScore(String sno, Connection conn) {
        FinalGrade finalGrade = new FinalGrade();
        try {
            String sql = "select" +
                    " sno, grade1, grade1_id, grade2, grade2_id, grade3, grade3_id, final_grade, pass_state, opinion, is_pass " +
                    " from final_grade where sno=?";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, sno);
            rs = ps.executeQuery();
            if (rs.next()) {
                finalGrade.setSno(rs.getString("sno"));
                finalGrade.setGrade1(rs.getInt("grade1"));
                finalGrade.setGrade1Id(rs.getInt("grade1_id"));
                finalGrade.setGrade2(rs.getInt("grade2"));
                finalGrade.setGrade2Id(rs.getInt("grade2_id"));
                finalGrade.setGrade3(rs.getInt("grade3"));
                finalGrade.setGrade3Id(rs.getInt("grade3_id"));
                finalGrade.setFinalGrade(rs.getDouble("final_grade"));
                finalGrade.setPassState(rs.getString("pass_state"));
                finalGrade.setOpinion(rs.getString("opinion"));
                finalGrade.setIsPass(rs.getString("is_pass"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return finalGrade;
    }

}
